package com.aorise.controller.general;

import com.aorise.exception.DataValidationException;
import com.aorise.service.general.GeneralService;
import com.aorise.utils.DataValidation;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/3
 * @Description:    运行一览图控制器
 */
@Api(description = "运行一览图控制器")
@RestController
@RequestMapping("api/general")
@Slf4j
@EnableScheduling
@RequiresPermissions("YXYLTFW")
public class GeneralController {

    @Autowired
    GeneralService generalService;

    /**
     * 运行一览图
     * HTTP 方式：GET
     * API 路径：/api/general/runGeneral
     * 方法名：getRunGeneral
     * 方法返回类型：String
     */
    @ApiOperation(value = "运行一览图", httpMethod = "GET", response = String.class, notes = "运行一览图")
    @RequestMapping(value = "/runGeneral",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRunGeneral(){
        List<Map<String,String>> generalMap =null;
        try{
             generalMap =generalService.getRunGeneral();
        }catch (DataAccessException e) {
            log.error(
                    "controller:GeneralController. function:getProGeneral...msg: getProGeneral DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:GeneralController. function:getProGeneral. msg:getProGeneral Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofSuccess(generalMap);
    }

    /**
     * 七日在线率
     * HTTP 方式：GET
     * API 路径：/api/general/onlineRate
     * 方法名：getOnlineRate
     * 方法返回类型：String
     */
    @ApiOperation(value = "七日在线率", httpMethod = "GET", response = String.class, notes = "七日在线率")
    @RequestMapping(value = "/onlineRate",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResponseData getOnlineRate(){
        List<Map<String,String>> onlineMap =null;
        try {
             onlineMap =generalService.getOnlineRate();
        }catch (DataAccessException e) {
            log.error(
                    "controller:GeneralController. function:getProGeneral...msg: getProGeneral DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null);
        } catch (Exception e) {
            log.error(
                    "controller:GeneralController. function:getProGeneral. msg:getProGeneral Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null);
        }
        return new JsonResponseData(Boolean.TRUE, "",
                StatusDefine.SUCCESS, StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), onlineMap);
    }

    /**
     * 业务一览图
     * HTTP 方式：GET
     * API 路径：/api/general/proGeneral
     * 方法名：getProGeneral
     * 方法返回类型：String
     */
    @RequiresPermissions("YWYLTFW")
    @ApiOperation(value = "业务一览图", httpMethod = "GET", response = String.class, notes = "业务一览图")
    @RequestMapping(value = "/proGeneral",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProGeneral(){
        Map map= null;
        try {
            map = generalService.getProGeneral();
        } catch (DataAccessException e) {
            log.error(
                    "controller:GeneralController. function:getProGeneral...msg: getProGeneral DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:GeneralController. function:getProGeneral. msg:getProGeneral Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofSuccess(map);
    }

    /**
     * 运维统计
     * HTTP 方式：GET
     * API 路径：/api/general/operationSta
     * 方法名：getOperationSta
     * 方法返回类型：String
     */
    @RequiresPermissions("YWYLTFW")
    @ApiOperation(value = "运维统计", httpMethod = "GET", response = String.class, notes = "运维统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dateTime", value = "统计月份 example:2019-06",paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/operationSta",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getOperationSta(String dateTime) {
        Map map = null;
        DataValidation dataValidation = new DataValidation();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            dataValidation.chekeNotempty(dateTime,StatusDefineMessage.GetMessage(StatusDefine.ISNULLPARAM));
            dataValidation.checkDateTime(dateTime, StatusDefineMessage.GetMessage(StatusDefine.DATA_FORMAT_ERROR));
            Date date = sdf.parse(dateTime);
            map = generalService.getOperationSta(date);
        }catch (DataValidationException e) {
            log.error(
                    "controller:GeneralController. function:getOperationSta...msg: getOperationSta DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:GeneralController. function:getOperationSta...msg: getOperationSta DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:GeneralController. function:getOperationSta. msg:getOperationSta Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofSuccess(map);
    }


}
